// pages/create_room/create_room.js
const app = getApp()
// const db = cloud.database()
const util = require('../utils/utils')
let rooms = []
let tasks = []
Page({

  /**
   * 页面的初始数据
   */
  data: {
    room_name: '',
    room_password: '',
    is_ok: '',//名字和密码是否输入完成
    show_message: '',
    // is_choice: false,
    mode:0,
    is_submit:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

   
  },  


  input_name(e) {
    let temp = e.detail.value
    this.setData({
      room_name: temp,
      is_submit:false
    })

  },
   input_password(e) {
    this.setData({
      room_password: e.detail.value,
      is_submit:false
    })
  },
  create_tasks() {
    let tasks_center = []
    wx.cloud.callFunction({
      name: 'get_datas',  // 云函数名称
      data: {  // 传给云函数的参数
        name: 'task_center',
        filter: {}  //筛选条件，默认为空（即查询所有记录），格式 {key:'values'} 
      },
      complete: res => {
        tasks_center = res.result.data
        // console.log(tasks_center);
        let cnt = 0
        let array = new Array(104).fill(0)
        while (cnt<25){
          let idx = parseInt((Math.random() * 100));
          if (array[idx] == 0 && idx<96) {
            let temp = {}
            temp.color = 'white'
            temp.name = tasks_center[idx].tasks[0]
            tasks[cnt++] = temp
            array[idx] = 1
          }
        }
      }
    })
  },
  choice_mode(e) {
    let temp = parseInt(e.target.dataset.choice)
    this.setData({
      // is_choice: true,
      mode:temp
    })
    console.log(temp);
  },
  bindsubmit(e) {
    let mode = this.data.mode
    this.setData({is_submit:true})
    if (mode === 0) {
      this.setData({
        is_ok: e.currentTarget.dataset.target,
        show_message: "未选择房间模式"
      })
    } else {
      let tempname = this.data.room_name
      let temppassword = this.data.room_password
      let flag = false;
      let temp_create_time = new Date().getTime()
      console.log("temp_update_time",temp_create_time);
      // let tasks = []  //随机生成任务函数
      // for (let i = 0; i < 25; i++) {
      //   tasks.push({ "name": (i * 243 + 1).toString(), "color": "white" })
      // }
      this.create_tasks()
      if (tempname == '' || temppassword == '') {
        this.setData({
          is_ok: e.currentTarget.dataset.target,
          show_message: "房间名字或者密码未设置成功，请重新输入"
        })
      } else {
        let that = this
        wx.cloud.callFunction({
          name: 'get_datas',  // 云函数名称
          data: {  // 传给云函数的参数
            name: 'room',
            filter: {}  //筛选条件，默认为空（即查询所有记录），格式 {key:'values'} 
          },
          complete: res => {
            rooms = res.result.data
            // console.log("rooms:", rooms, that.data.room_name);
            for (let i = 0; i < rooms.length; i++) {
              if (rooms[i].room_name == that.data.room_name) {
                flag = true
                break
              }
            }
            if (flag == false) {
             
              wx.cloud.callFunction({
                // 需调用的云函数名
                name: 'create_room',
                // 传给云函数的参数
                data: {
                  room_name: that.data.room_name,
                  room_password: that.data.room_password,
                  users: {},
                  ready_num: 0,
                  viewer: 0,
                  tasks: tasks,
                  mode: that.data.mode,
                  room_state: false,
                  is_start: false,
                  create_time:temp_create_time
                },
                // 成功回调
                complete: res => {
                  let temp_room = {}
                  temp_room._id = res.result._id
                  temp_room.room_name = that.data.room_name
                  temp_room.room_password = that.data.room_password
                  app.globalData.room = temp_room
                  rooms.push(temp_room)
                  app.globalData.rooms = rooms
                  // console.log(rooms);
                  wx.navigateTo({
                    url: '/pages/room_center/room_center',
                  });
                }
              })
          
            } else {
              this.setData({
                is_ok: e.currentTarget.dataset.target,
                show_message: '房间名重复请重新输入'
              })
            }
          }
        })
      }
    }
  },
  hideModal(e) {
    this.setData({
      is_ok:null
    })
  }
})